package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceAddWorkpartAdapter;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.iview.IGetProductWorkPartListView;
import com.cloudsigns.factorymanager2.produce.presenter.GetProductWorkPartListPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

public class ProduceAddWorkpartActivity extends BaseActivity implements IGetProductWorkPartListView  {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private ListViewForScrollView lv;
    private Button btn_add;

    private ProduceAddWorkpartAdapter adapter;
    private List<WorkPartBean> workPartList = new ArrayList<>();
    private GetProductWorkPartListPresenter getProductWorkPartListPresenter;
    private int selectedPositon;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_add_workpart);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        lv = findViewById(R.id.lv);
        btn_add = findViewById(R.id.btn_add);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.add_process));
        getProductWorkPartListPresenter = new GetProductWorkPartListPresenter(this);
        getProductWorkPartListPresenter.getProductWorkPartList();

        adapter = new ProduceAddWorkpartAdapter(this, TAG);
        adapter.setData(workPartList);
        lv.setAdapter(adapter);
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        btn_add.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
                // 添加
            case R.id.btn_add:
                boolean isNotSelected = true;
                List<WorkPartBean> list = workPartList;
                ArrayList<WorkPartBean> workpartSelectedList = new ArrayList< >();
                boolean isLastExist=false;
                for (WorkPartBean productBean : list) {
                    if (productBean.isChecked) {
                        isNotSelected = false;
                        workpartSelectedList.add(productBean);
                        if(productBean.isLastWorkPart()){
                            isLastExist=true;
                        }
                    }
                }
                if (isNotSelected) {
                    showToast(getResources().getString(R.string.select_process));
                }else if(!isLastExist){
                    showToast(getResources().getString(R.string.select_process));
                } else{
                    Intent mIntent = new Intent();
                    mIntent.putExtra(ConstantUtil.KEY_2, workpartSelectedList);
                    setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
                    finish();
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            switch (requestCode) {
                case ConstantUtil.START_ACTIVITY_KEY_1:
                    if (null != getProductWorkPartListPresenter)
                        getProductWorkPartListPresenter.getProductWorkPartList();
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void getDataSuccessed(List<WorkPartBean> workPartLs ) {
        if (null != workPartLs && !workPartLs.isEmpty()) {
            workPartList.clear();
            workPartList.addAll(workPartLs);
            adapter.setData(workPartList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showFailedError(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
